package com.report.controller.admin.vote;

import com.report.common.model.Result;
import com.report.common.util.StringUtil;
import com.report.controller.ai.CategoryController;
import com.report.dao.admin.vote.VoteMapper;
import com.report.dao.report.ReportMapper;
import com.report.model.admin.vote.Vote;
import com.report.model.admin.vote.VoteExample;
import com.report.model.user.User;
import com.report.model.wp.Category;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/vote")
public class VoteController {

    @Autowired
    private VoteMapper voteMapper;

    Logger log = Logger.getLogger(VoteController.class);

    final String batch = "201903SHEJI";

    /**
     * 查询分类树形结构
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/submit")
    public Result submit(@RequestBody Map<String, Object> params, HttpSession session) {

        Result result = new Result();
        User user = (User) session.getAttribute("user");
        if (null == user) {
            result.setState(-1);
            result.setMsg("尚未登录");
            return result;
        }
        int userId = user.getId();
        //查询该用户是否已投票
        VoteExample ex = new VoteExample();
        VoteExample.Criteria cr = ex.createCriteria();
        cr.andBatchEqualTo(batch);
        cr.andUserIdEqualTo(userId);
        List<Vote> list = voteMapper.selectByExample(ex);
        if (list.size() > 0) {
            result.setMsg("您已经投过票，不能重复投票");
            result.setState(-1);
            return result;
        }
        String username = user.getDisplayName();
        List<Map<String, Object>> voteData = (List<Map<String, Object>>) params.get("voteData");
        try {
            for (Map<String, Object> map : voteData) {
                Vote v = new Vote();
                v.setBatch(batch);
                Object point = map.get("point");
                if (point != null && point != "") {
                    v.setPoint((Integer) point);
                }else{
                    result.setMsg("请给所有同事都打上分数");
                    result.setState(-1);
                    return result;
                }
//                if(StringUtil.isNotEmpty(point)){
//                    v.setPoint(Integer.parseInt(point));
//                }
                v.setRemark((String) map.get("remark"));
                v.setVoteName((String) map.get("name"));
                v.setUserId(userId);
                v.setUserName(username);
                voteMapper.insertSelective(v);
            }
        } catch (Exception e) {
            result.setMsg("添加失败:" + e.getMessage());
            result.setState(-1);
        }
        return result;
    }


    /**
     * 查询绩效结果
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/result")
    public Result queryVoteResult(HttpSession session) {
        Result result = new Result();
        User user = (User) session.getAttribute("user");
        if (null == user) {
            result.setState(-1);
            result.setMsg("尚未登录");
            return result;
        }
        int userId = user.getId();
        //只有特定用户才可查看
        List<Integer> whiteList = Arrays.asList(3, 5, 31);
        if (!whiteList.contains(userId)) {
            result.setState(-1);
            result.setMsg("无权限查看");
            return result;
        }
        //查询结果
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("batch", batch);
        List<Map<String, Object>> list = voteMapper.queryVoteResult(params);
        Map<String, Object> r = new HashMap<String, Object>();
        r.put("list", list);
        result.setResult(r);
        return result;
    }
}
